//
//  RightRestaurantTableViewCell.m
//  Park
//
//  Created by 123456 on 2017/12/5.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "RightRestaurantTableViewCell.h"
#import "CategoryModel.h"
#import "UIImageView+WebCache.h"

@implementation RightRestaurantTableViewCell
{
    int i;//记录数量
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

    [self Layout];
    self.ReduceBUtton.hidden = YES;
    self.NumberLabel.hidden = YES;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.imageV.contentMode = UIViewContentModeScaleAspectFill;
    self.imageV.layer.cornerRadius = 5;
    self.imageV.layer.masksToBounds = YES;
    self.amount = 0;
}


- (void)setModel:(FoodModel *)model
{
    
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:model.picture]];
    self.nameLabel.text = model.name;
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %.2f",[model.price floatValue]];
    self.NumberLabel.text = [NSString stringWithFormat:@"%@",model.amount];
    self.amount =model.amount.integerValue;
        
    self.amount = model.amount.integerValue?model.amount.integerValue:0;
    
    [self showOrderNumbers:self.amount];

    [self.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IPHEAD,model.image]] placeholderImage:[UIImage imageNamed:@""]];

//    self.SalesVolumeLabel.text = [NSString stringWithFormat:@"月销:%@",model.bookingsNub];
    self.DetailsLabel.text =[NSString stringWithFormat:@"月销:%@",model.bookingsNub];
//    self.DetailsLabel.text = [NSString stringWithFormat:@"%@",model.remark];
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state


}


- (IBAction)plusBUtton:(id)sender {
    
    if ([self isBetweenFromHour:8 toHour:20] ==YES) {

    self.amount += 1;
    self.plusBlock(self.amount,YES,self.orderid);
    [self showOrderNumbers:self.amount];
    }else{
        UIWindow * window = [UIApplication sharedApplication].keyWindow;
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:@"抱歉本店已经打烊，请在早上8:00-晚上20:00时间段内下单！" toView:window];
    }
}


- (IBAction)ReduceBUtton:(id)sender {
    self.amount -= 1;
    
    self.plusBlock(self.amount,NO,self.orderid);

    [self showOrderNumbers:self.amount];


}

//-(void)layoutSubviews
//{
//    [super layoutSubviews];
//
//    [self showOrderNumbers:self.amount];
//
//}

-(void)showOrderNumbers:(NSUInteger)count
{
    
    self.NumberLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)count];

    if (self.amount > 0)
    {
        [self.ReduceBUtton setHidden:NO];
        [self.NumberLabel setHidden:NO];
    }
    else
    {
        [self.ReduceBUtton setHidden:YES];
        [self.NumberLabel setHidden:YES];
    }
}

-(void)Layout
{
    
    if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {

        self.ReduceH.constant = 23;
        self.ReduceW.constant = 23;
        self.plusH.constant = 21;
        self.plusW.constant =21;
        self.imageH.constant = 75;
        self.imageW.constant = 75;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){
        
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")){
        
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"812.0")){
        
        
    }
    
    
    
}



//判断当前时间是否在某时间段内
- (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour {
    
    NSDate *dateFrom = [self getCustomDateWithHour:fromHour];
    NSDate *dateTo = [self getCustomDateWithHour:toHour];
    
    NSDate *currentDate = [NSDate date];
    if ([currentDate compare:dateFrom]==NSOrderedDescending && [currentDate compare:dateTo]==NSOrderedAscending) {
        // 当前时间在9点和10点之间
        return YES;
    }
    return NO;
}

- (NSDate *)getCustomDateWithHour:(NSInteger)hour {
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *currentComps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
    
    //设置当天的某个点
    NSDateComponents *resultComps = [[NSDateComponents alloc] init];
    [resultComps setYear:[currentComps year]];
    [resultComps setMonth:[currentComps month]];
    [resultComps setDay:[currentComps day]];
    [resultComps setHour:hour];
    
    NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    return [resultCalendar dateFromComponents:resultComps];
}





@end
